const webpack = require("webpack");
const path = require("path");
const CopyPlugin = require("copy-webpack-plugin");
const nodeExternals = require("webpack-node-externals");
const JavaScriptObfuscator = require("webpack-obfuscator");

module.exports = (env, options) => {
  return {
    entry: "./src/app.ts",
    target: "node",
    externals: [nodeExternals()],
    output: {
      path: path.resolve(__dirname, "build"),
      filename: "server.js",
    },

    module: {
      rules: [
        {
          test: /\.ts$/,
          use: "ts-loader",
          exclude: /node_modules/,
        },
      ],
    },
    resolve: {
      alias: {
        "@": path.resolve(__dirname, "src/"),
      },
      extensions: [".ts", ".js"],
    },

    plugins: [
      new CopyPlugin({
        patterns: [
          {
            from: "config/**/*.pem",
            context: path.resolve(__dirname, "src"),
          },
        ],
      }),
      new JavaScriptObfuscator({
        rotateUnicodeArray: true,
      }),
    ],
  };
};
